package team.freeroom.hotelmanage.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.freeroom.hotelmanage.controller.model.Page;
import team.freeroom.hotelmanage.repository.entity.Room;
import team.freeroom.hotelmanage.service.FloorService;
import team.freeroom.hotelmanage.service.RoomService;
import team.freeroom.hotelmanage.service.RoomTypeService;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/room")
@Controller
public class RoomController {

    @Autowired
    private RoomTypeService roomTypeService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private FloorService floorService;

    /**
     * 房间管理列表页面
     */
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.addObject("roomTypeList", roomTypeService.findAll());
        model.addObject("floorList", floorService.findAll());
        model.setViewName("room/room");
        return model;
    }

    /**
     * 房间信息添加操作
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Room room){
        Map<String, String> ret = new HashMap<String, String>();
        if(room == null){
            ret.put("type", "error");
            ret.put("msg", "请填写正确的房间信息!");
            return ret;
        }
        if(StringUtils.isEmpty(room.getSn())){
            ret.put("type", "error");
            ret.put("msg", "房间编号不能为空!");
            return ret;
        }
        if(room.getRoomTypeId() == null){
            ret.put("type", "error");
            ret.put("msg", "请选择房间类型!");
            return ret;
        }
        if(room.getFloorId() == null){
            ret.put("type", "error");
            ret.put("msg", "请选择房间所属楼层!");
            return ret;
        }
        if(isExist(room.getSn(), 0l)){
            ret.put("type", "error");
            ret.put("msg", "该房间编号已经存在!");
            return ret;
        }
        if(roomService.add(room) <= 0){
            ret.put("type", "error");
            ret.put("msg", "添加失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "添加成功!");
        return ret;
    }

    /**
     * 房间信息编辑操作
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Room room){
        Map<String, String> ret = new HashMap<String, String>();
        if(room == null){
            ret.put("type", "error");
            ret.put("msg", "请填写正确的房间信息!");
            return ret;
        }
        if(StringUtils.isEmpty(room.getSn())){
            ret.put("type", "error");
            ret.put("msg", "房间编号不能为空!");
            return ret;
        }
        if(room.getRoomTypeId() == null){
            ret.put("type", "error");
            ret.put("msg", "请选择房间类型!");
            return ret;
        }
        if(room.getFloorId() == null){
            ret.put("type", "error");
            ret.put("msg", "请选择房间所属楼层!");
            return ret;
        }
        if(isExist(room.getSn(), room.getId())){
            ret.put("type", "error");
            ret.put("msg", "该房间编号已经存在!");
            return ret;
        }
        if(roomService.edit(room) <= 0){
            ret.put("type", "error");
            ret.put("msg", "修改失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "修改成功!");
        return ret;
    }

    /**
     * 分页查询房间信息
     */
    @RequestMapping(value="/list",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(
            @RequestParam(name="sn",defaultValue="") String sn,
            @RequestParam(name="status",required=false) Integer status,
            @RequestParam(name="roomTypeId",required=false) Long roomTypeId,
            @RequestParam(name="floorId",required=false) Long floorId,
            Page page
    ){
        Map<String,Object> ret = new HashMap<String, Object>();
        Map<String,Object> queryMap = new HashMap<String, Object>();
        queryMap.put("sn", sn);
        queryMap.put("status", status);
        queryMap.put("roomTypeId", roomTypeId);
        queryMap.put("floorId", floorId);
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", roomService.findList(queryMap));
        ret.put("total", roomService.getTotal(queryMap));
        return ret;
    }

    /**
     * 房间信息删除操作
     */
    @RequestMapping(value="/delete",method= RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(Long id){
        Map<String, String> ret = new HashMap<String, String>();
        if(id == null){
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的信息!");
            return ret;
        }
        try {
            if(roomService.delete(id) <= 0){
                ret.put("type", "error");
                ret.put("msg", "删除失败，请联系管理员!");
                return ret;
            }
        } catch (Exception e) {
            ret.put("type", "error");
            ret.put("msg", "该房间下存在订单信息，请先删除该房间下的所有订单信息!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "删除成功!");
        return ret;
    }

    /**
     * 判断房间编号是否存在
     */
    private boolean isExist(String sn,Long id){
        Room findBySn = roomService.findBySn(sn);
        if(findBySn == null)return false;
        if(findBySn.getId().longValue() == id.longValue())return false;
        return true;
    }
}
